package com.itheima.controller;

import com.itheima.utils.AliyunOSSOperator;
import com.itheima.pojo.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UpLoadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("文件上传,文件名:{}",file.getOriginalFilename());
        if(!file.isEmpty()){
            String originalFilename = file.getOriginalFilename();
            String exFileName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString().replace("-", "")+exFileName;
            String url = aliyunOSSOperator.upload(newFileName, file.getBytes());
            return Result.success(url);
        }
        return Result.error("上传失败，文件为空！");
    }
}
